package com.njxzc.igo.controller.products;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.njxzc.igo.POJO.ProductsDTO;
import com.njxzc.igo.controller.shoppingcart.ReloadShoppingCart;
import com.njxzc.igo.model.Products;
import com.njxzc.igo.model.User;
import com.njxzc.igo.service.GoodsService;
import com.njxzc.igo.service.GoodsServiceImpl;

@WebServlet(name="loaditem",urlPatterns="/item.do")
public class LoadItemDetailsController extends ReloadShoppingCart {
	private GoodsService gs  = new GoodsServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			HttpSession session = request.getSession();
			User u = (User) session.getAttribute("u");
			if(u!=null){
				Map shoppingCart = reloadShoppingCart(u);
				session.setAttribute("map3", shoppingCart);	
			}
			
			long productsId = Long.parseLong(request.getParameter("productId"));
			
			List<ProductsDTO> list = (List<ProductsDTO>) getServletContext().getAttribute("productsTickets");
			for(ProductsDTO pd :list){
				if(pd.getProductId()==productsId){
					long temp = pd.getClickAmount();
					pd.setClickAmount(++temp);
				}
			}
			
			
			Products product = gs.loadItemDetails(productsId);
			request.setAttribute("item", product);
			request.getRequestDispatcher("WEB-INF/item.jsp").forward(request, response);
	}
	
}
